import Users.Role.Administrator;
import Users.Role.NormalUsers;
import Users.User;
import entity.Book;
import entity.BookList;

import java.util.Scanner;

/**
 * 程序入口
 */
public class Main {
    // 设置默认系统用户及其密码
    static final String ADMIN = "Cup";
    static final String PASSWORD = "123456";

    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args){
        User user = null;
        try {
            user = login();
            System.out.println("登录成功！");
        } catch (UsernameException e) {
            e.printStackTrace();
        } catch (PasswordException e) {
            e.printStackTrace();
        }
        if (user != null) {
            while (true) {
                int select = user.UI();
                if ((user instanceof Administrator && select == 5) || (user instanceof NormalUsers && select == 4)) {
                    System.out.println("退出成功");
                    return;
                }
                BookList bookList = new BookList();
                user.operations(select, bookList);
            }
        }
    }

    /**
     * 登录
     * @return
     */
    public static User login() throws UsernameException, PasswordException{
        System.out.println("欢迎使用(*^▽^*)");
        System.out.println("请输入您的身份：");
        System.out.println("***************************************************");
        System.out.println("1.管理员");
        System.out.println("2.普通用户");
        System.out.println("***************************************************");
        int select = sc.nextInt();
        if (select == 1) {
            System.out.println("请输入管理员名称：");
            String name = sc.next();
            if (!ADMIN.equals(name)) {
                throw new UsernameException("该管理员不存在！");
            }
            System.out.println("请输入您的密码：");
            String password = sc.next();
            if (!PASSWORD.equals(password)) {
                throw new PasswordException("密码输入错误！");
            }
            return new Administrator(name);
        } else if(select == 2) {
            System.out.println("请输入用户名称：");
            String name = sc.next();

            return new NormalUsers(name);
        }
        System.out.println("登录失败！");
        return null;
    }
}

// 用户名异常
class UsernameException extends Exception {
    public UsernameException(String msg) {
        super(msg);
    }
}

// 密码异常
class PasswordException extends RuntimeException {
    public PasswordException(String msg) {
        super(msg);
    }
}
